package com.mhwx.mhlive.ui.activity;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.LinearLayout;

import com.mhwx.mhlive.R;
import com.mhwx.mhlive.presenter.UserLoginPresenter;
import com.mhwx.mhlive.ui.base.BaseActivity;
import com.mhwx.mhlive.util.IntoActivity;
import com.mhwx.mhlive.util.ShareSdkUtil;
import com.mhwx.mhlive.view.UserLoginView;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.utils.SocializeUtils;

import java.util.Map;



/**
 * 类功能描述
 *
 * @author Guol
 * @version 1.0
 * @copyright 北京明华维新信息技术有限公司
 * @time 2017/4/24
 */
public class LoginActivity extends BaseActivity implements UserLoginView,View.OnClickListener,UMAuthListener {

    LinearLayout ll_wx,ll_phone,ll_qq,ll_sina;


    String threeLoadId ,threeLoadPic,threeLoadName,threeLoadGender;

    Dialog dialog;

   private UserLoginPresenter mLoginPresenter;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_login);
        init();

    }

    private  void init(){
        dialog = new ProgressDialog(this);
        mLoginPresenter = new UserLoginPresenter(this,LoginActivity.this);
        ll_sina=(LinearLayout) findViewById(R.id.ll_sina);
        ll_qq=(LinearLayout) findViewById(R.id.ll_qq);
        ll_wx=(LinearLayout) findViewById(R.id.ll_wx);
        ll_phone=(LinearLayout) findViewById(R.id.ll_phone);
        ll_sina.setOnClickListener(this);
        ll_qq.setOnClickListener(this);
        ll_wx.setOnClickListener(this);
        ll_phone.setOnClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    public void showLoading() {
       dialog.show();
    }

    @Override
    public void closeLoading() {
        dialog.dismiss();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ll_wx:
                ShareSdkUtil.loginThirdPlatform(LoginActivity.this, SHARE_MEDIA.WEIXIN,this);
                break;
            case R.id.ll_qq:
                ShareSdkUtil.loginThirdPlatform(LoginActivity.this, SHARE_MEDIA.QQ,this);
                break;
            case R.id.ll_sina:
                ShareSdkUtil.loginThirdPlatform(LoginActivity.this, SHARE_MEDIA.SINA,this);
                break;
            case R.id.ll_phone:
                IntoActivity.toNextActivity(LoginActivity.this  , MainActivity.class);
                break;
        }
    }

    @Override
    public void onStart(SHARE_MEDIA share_media) {
        //授权开始回调
        SocializeUtils.safeShowDialog(dialog);
    }

    @Override
    public void onComplete(SHARE_MEDIA share_media, int i, Map<String, String> map) {
       //成功
        threeLoadId = map.get("uid");
        threeLoadPic = map.get("iconurl");
        threeLoadName = map.get("name");
        threeLoadGender=map.get("gender");
        SocializeUtils.safeCloseDialog(dialog);
        mLoginPresenter.login();
    }

    @Override
    public void onError(SHARE_MEDIA share_media, int i, Throwable throwable) {
      //失败
        SocializeUtils.safeCloseDialog(dialog);
    }

    @Override
    public void onCancel(SHARE_MEDIA share_media, int i) {
        SocializeUtils.safeCloseDialog(dialog);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        UMShareAPI.get(this).release();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        UMShareAPI.get(this).onSaveInstanceState(outState);
    }
}
